<?php
namespace App\Http\Controllers\Admin\Content;

use App\Enums\ResponseEnum;
use App\Http\Controllers\AdminController;
use App\Models\Content\AttachmentGroup;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FilesGroupController extends AdminController
{

    public function data(Request $request)
    {
        $model = AttachmentGroup::query()->select(['id', 'name', 'sort', 'status', 'created_at', 'updated_at']);
        if ($keywords = $request->get('keywords')) {
            $model->where('name', 'like', filterStr($keywords).'%');
        }
        $list = $model->orderBy('sort')->paginate($request->get('limit', 10));
        return $this->succeed([
            'total' => $list->total(),
            "rows" => $list->items()
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|unique:attachment_group|max:50',
        ]);
        $data = $request->only(['sort', 'name', 'status']);
        try {
            DB::transaction(function () use ($data) {
                $model = AttachmentGroup::query()->create([
                    'sort' => (int)$data["sort"],
                    'name' => $data["name"],
                    'status' => (int)$data["status"],
                ]);
                $this->createSyslog('添加附件分组', $model);
            });
            return $this->succeed();
        }catch (\Exception $e) {
            report($e);
        }
        return $this->error();
    }

    public function update($id, Request $request)
    {
        $request->validate([
            'name' => 'required|unique:attachment_group,name,'.$id.',id|max:50',
        ]);
        $item = AttachmentGroup::query()->findOrFail($id);
        $data = $request->only(['sort', 'name', 'status']);
        try {
            DB::transaction(function () use ($item, $data) {
                $item->update([
                    'sort' => (int)$data["sort"],
                    'name' => $data["name"],
                    'status' => (int)$data["status"],
                ]);
                $this->createSyslog('修改附件分组', $item);
            });
            return $this->succeed();
        }catch (\Exception $e) {
            report($e);
        }
        return $this->error();
    }

    public function destroy(Request $request)
    {
        $request->validate(['ids' => 'required|array|size:'.static::QUERY_LIMIT]);
        $ids = $request->get('ids');
        $list = AttachmentGroup::query()->select(['id'])->whereIn('id', $ids)->withCount('files')->limit(static::QUERY_LIMIT)->get();
        abort_if($list->isEmpty(), ResponseEnum::DELETE_NOT_EXIST_ERROR);
        try {
            DB::transaction(function () use ($list) {
                foreach ($list as $model) {
                    abort_if($model->files_count > 0, ResponseEnum::ATTACHMENT_THE_GROUP_ERROR);
                    $model->delete();
                    $this->createSyslog('删除附件分组', $model);
                }
            });
            return $this->succeed();
        }catch (\Exception $e) {
            report($e);
        }
        return $this->error();
    }

}
